package spuStoreWeighRecord

import (
	"context"
	"errors"
	"github.com/google/uuid"
	"github.com/shopspring/decimal"
	"insighttracking/apps/purchase/auth"
	"insighttracking/query/service"
	"net/http"

	"insighttracking/apps/purchase/internal/svc"
	"insighttracking/apps/purchase/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
	"os"
)

type CreateSpuStoreWeighRecordApiLogic struct {
	logx.Logger
	ctx     context.Context
	svcCtx  *svc.ServiceContext
	Request *http.Request
}

func NewCreateSpuStoreWeighRecordApiLogic(ctx context.Context, svcCtx *svc.ServiceContext, request *http.Request) *CreateSpuStoreWeighRecordApiLogic {
	hostname, _ := os.Hostname()
	return &CreateSpuStoreWeighRecordApiLogic{
		Logger: logx.WithContext(ctx).WithCallerSkip(0).WithFields(
			logx.Field("Log_UUID", uuid.New().String()),
			logx.Field("hostname", hostname)),
		ctx:     ctx,
		svcCtx:  svcCtx,
		Request: request,
	}
}

func (l *CreateSpuStoreWeighRecordApiLogic) CreateSpuStoreWeighRecordApi(req *types.CreateSpuStoreWeighRecordReq) (resp string, err error) {
	accountInfo, err := auth.GetAccountInfo(l.svcCtx, l.Request)
	if err != nil {
		return "", err
	}
	weight, _ := decimal.NewFromString(req.Weight)
	if err = service.SpuStoreWeighRecordService.CreateSpuStoreWeighRecordApi(l.ctx, accountInfo.EnterpriseId, req.PurchaseStoreDetailId, req.SpuID, weight); err != nil {
		return "", errors.New("操作失败")
	}
	return "", nil
}
